import sys;input = sys.stdin.readline
from collections import deque
I = lambda :int(input())
L = lambda :list(map(int, input().split()))
T = lambda :map(int, input().split())
n = I()
graph = [set() for i in range(n+1)]
for i in range(n-1):
a, b = T()
graph[a].add(b)
graph[b].add(a)
order = L()
q = deque([1])
v = [0]*(n+1)
size = [0]*(n+1)
v[1] = 1
while q:
node = q.popleft()
for nbr in graph[node]:
if v[nbr] == 0:
q.append(nbr)
v[nbr] = 1
size[node] += 1
j = 1
for index in range(n):
node = order[index]
for k in range(size[node]):
if j<=n:
child = order[j]
if child not in graph[node]:
print("No")
sys.exit()
j += 1
else:
print("No")
sys.exit()
print("Yes")
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> g[N];
queue<int> q;
int a[N], par[N], pos[N];
bool mark[N];
void bfs(int x) {
q.push(x);
mark[x] = true;
par[x] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v: g[u]) {
if (!mark[v]) {
q.push(v);
mark[v] = true;
par[v] = u;
}
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
bfs(1);
int pnt = 0, cp = 0, mxpos = 0;
for (int i = 0; i < n; i++) {
cp = par[a[i]];
while (i < n && par[a[i]] == cp) {
i++;
}
i--;
int tmp = i - pnt;
pnt = i;
int sz = g[cp].size();
if (cp != 1 && cp != 0)
sz--;
if (tmp != sz || pos[cp] < mxpos) {
cout << "No" << '\n';
exit(0);
}
mxpos = pos[cp];
}
cout << "Yes" << '\n';
}
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |
1688. Count of Matches in Tournament | 1684. Count the Number of Consistent Strings |
1588. Sum of All Odd Length Subarrays | 1662. Check If Two String Arrays are Equivalent |
1832. Check if the Sentence Is Pangram | 1678. Goal Parser Interpretation |
1389. Create Target Array in the Given Order | 1313. Decompress Run-Length Encoded List |
1281. Subtract the Product and Sum of Digits of an Integer | 1342. Number of Steps to Reduce a Number to Zero |
1528. Shuffle String | 1365. How Many Numbers Are Smaller Than the Current Number |
771. Jewels and Stones | 1512. Number of Good Pairs |
672. Richest Customer Wealth | 1470. Shuffle the Array |
1431. Kids With the Greatest Number of Candies | 1480. Running Sum of 1d Array |
682. Baseball Game | 496. Next Greater Element I |
232. Implement Queue using Stacks | 844. Backspace String Compare |
20. Valid Parentheses | 746. Min Cost Climbing Stairs |
392. Is Subsequence | 70. Climbing Stairs |